function validateFirstName(){
    var valid = true;
    var re =/^([^\s\!\@\#\$\%\^\&\*\(\)\-\_\=\+\]\}\[\{\'\"\;\:\?\.\>\,\<0-9]*)$/;
    if ( document.getElementById("firstName").value =="" )
    {
    	document.getElementById("erFirstName").innerHTML = "<img src=\"Images/error.png\" class=\"images\"/> Fill in your first name!";
        valid = false;
    }
    else if(!document.getElementById("firstName").value.match(re))
    {
    	document.getElementById("erFirstName").innerHTML = "<img src=\"Images/error.png\" class=\"images\"/> Invalid expresion!";
        valid = false;
    }
    else
    {
    	document.getElementById("erFirstName").innerHTML = "<img src=\"Images/ok.png\" class=\"images\"/>";
    }
    return valid;
}

function validateLastName(){
	var re =/^([^\s\!\@\#\$\%\^\&\*\(\)\-\_\=\+\]\}\[\{\'\"\;\:\?\.\>\,\<0-9]*)$/;
    var valid = true; 
    if ( document.getElementById("lastName").value == "" )
    {
    	document.getElementById("erLastName").innerHTML = "<img src=\"Images/error.png\" class=\"images\"/> Fill in your last name!";
        valid = false;
    }
    else if(!document.getElementById("lastName").value.match(re))
    {
    	document.getElementById("erLastName").innerHTML = "<img src=\"Images/error.png\" class=\"images\"/> Invalid expresion!";
        valid = false;
    }
    else
    {
    	document.getElementById("erLastName").innerHTML = "<img src=\"Images/ok.png\" class=\"images\" />";
    }
    return valid;
}

function validateEmailAddress(){
	if(window.XMLHttpRequest)
    {
        xmlhttp = new XMLHttpRequest();
    }
	
    var valid = true;
    str = document.getElementById("emailAddress").value;
    var mail = /^([a-zA-Z0-9_\.\-])+\@([a-zA-Z0-9\-])+\.([a-zA-Z0-9]{2,4})$/;
    if ( document.getElementById("emailAddress").value == "" )
    {
    	document.getElementById("erEmailAddress").innerHTML = "<img src=\"Images/error.png\" class=\"images\"/> Fill in your email!";
    	valid = false;
    }
    else if (! document.getElementById("emailAddress").value.match(mail))
    {
    	document.getElementById("erEmailAddress").innerHTML = "<img src=\"Images/error.png\" class=\"images\"/> Email address not valid!";
	   	valid = false;
    }
    xmlhttp.onreadystatechange = function()
    {
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
            if(xmlhttp.responseText.length == 265)
                {
                    //the user already exists in the DB
            		document.getElementById("erEmailAddress").innerHTML = "<img src=\"Images/error.png\" class=\"images\"/> Email already in use!";
                    valid = false;
                }
        }
    };
    xmlhttp.open("GET","getUser.jsp?email="+str, true);
    xmlhttp.send();
    if(valid==true)
    {
    	document.getElementById("erEmailAddress").innerHTML = "<img src=\"Images/ok1.png\" />";
    }
    return valid;
}

function validatePassword(){
	var pas = /^([a-zA-Z0-9]*)$/;
    var valid = true;
    if ( !document.getElementById("password").value.match(pas) )
    {
    	document.getElementById("erPassword").innerHTML = "<img src=\"Images/error.png\" class=\"images\"/> Invalid password!";
        valid = false;
    }
    else if ( document.getElementById("password").value == "" )
    {
    	document.getElementById("erPassword").innerHTML = "<img src=\"Images/error.png\" class=\"images\"/> Choose a password!";
        valid = false;
    }
    else if(document.getElementById("password").value.length<6)
    {
    	document.getElementById("erPassword").innerHTML = "<img src=\"Images/error.png\" class=\"images\"/> Password too short(>6)!";
        valid = false;
    }
    else
    {
    	document.getElementById("erPassword").innerHTML = "<img src=\"Images/ok.png\" class=\"images\" />";
    }
    return valid;
}

function validateRetypedPassword(){
	valid = true;
    if ( document.getElementById("retypedPassword").value == "" )
    {
    	document.getElementById("erRetypedPassword").innerHTML = "<img src=\"Images/error.png\" class=\"images\"/> Retype the password!";
        valid = false;
    }
    else if(document.getElementById("password").value!=document.getElementById("retypedPassword").value)
    {
    	document.getElementById("erRetypedPassword").innerHTML = "<img src=\"Images/error.png\" class=\"images\"/> Don't match!";
    	valid =false;
    }
    else
    {
    	document.getElementById("erRetypedPassword").innerHTML = "<img src=\"Images/ok.png\" class=\"images\"/>";
    }
    return valid;
}

function validateCity(){
	var city=/^([^\s\!\@\#\$\%\^\&\*\(\)\-\_\=\+\]\}\[\{\'\"\;\:\?\.\>\,\<0-9]*)$/;
    var valid = true;
    if ( !document.getElementById("city").value.match(city) )
    {
    	document.getElementById("erCity").innerHTML = "<img src=\"Images/error.png\" class=\"images\"/> City invalid!";
        valid = false;
    }
    else 
    	if ( document.getElementById("city").value == "" )
    {
    	document.getElementById("erCity").innerHTML = "<img src=\"Images/error.png\" class=\"images\"/> Fill in the city field!";
        valid = false;
    }
    else{
    	document.getElementById("erCity").innerHTML = "<img src=\"Images/ok.png\" class=\"images\"/>";
    }
    return valid;
}

function validatePhoneNumber(){
    var valid = true;
    if ( document.getElementById("phoneNumber").value == "" )
    {
    	document.getElementById("erPhoneNumber").innerHTML = "<img src=\"Images/error.png\" class=\"images\"/> Fill in your phone number!";
        valid = false;
    }
    else{
    	document.getElementById("erPhoneNumber").innerHTML = "<img src=\"Images/ok.png\" class=\"images\"/>";
    }
    return valid;
}

function validate(){
	var valid = true;
	var errorMesage = "<ul>Please fill in the following fields correctly before registering:";
	if (validateFirstName()==false)
	{	
		errorMesage = errorMesage + "<li>First Name</li>";
	}
	if (validateLastName()==false)
	{	
		errorMesage = errorMesage + "<li>Last Name</li>";
	}
	if (validatePhoneNumber()==false)
	{
		errorMesage = errorMesage + "<li>Phone Number</li>";
	}
	if (validateCity()==false)
	{
		errorMesage = errorMesage + "<li>City</li>";
	}
	if (validateEmailAddress()==false)
	{	
		errorMesage = errorMesage + "<li>Email Address</li>";
	}
	if (validatePassword()==false)
	{	
		errorMesage = errorMesage + "<li>Password</li>";
	}
	if (validateRetypedPassword()==false)
	{	
		errorMesage = errorMesage + "<li>Retyped Password</li></ul>";
	}
	if (validateFirstName()==false || validateLastName()==false || validatePhoneNumber()==false || validateCity()==false || validateEmailAddress()==false || validatePassword()==false || validateRetypedPassword()==false)
	{	
		document.getElementById("errorMesageTab").style.display = "block";
		document.getElementById("errorMesageTab").innerHTML = errorMesage;
		valid = false;
	}
	else
	{
		valid = true;
	}
	return valid;
}

